import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:leader_doctor_flutter/common/utils/toast_utils.dart';
import 'package:leader_doctor_flutter/styles/colors.dart';
import '../../../common/http/api.dart';
import '../../../common/http/http_client.dart';
import '../../../common/http/url_helper.dart';
import '../../../common/utils/image_utils.dart';
import '../../../common/widget/comm_widget.dart';
import '../../../model/Protocol.dart';
import '../../../plugins/plugins_messages.g.dart';

/// @Author lixiaobin
/// @Date 2024/8/28
/// @Description 协议中心
class ProtocolListPage extends StatefulWidget {
  const ProtocolListPage({super.key});

  @override
  State<ProtocolListPage> createState() => _ProtocolListPageState();
}

class _ProtocolListPageState extends State<ProtocolListPage> {
  List<Protocol>? _protocolList;

  /// 获取协议列表
  @override
  void initState() {
    getProtocolList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var itemCount = _protocolList?.length ?? 0;
    return Scaffold(
        appBar: CommWidget.getCommAppbar(context),
        body: itemCount == 0
            ? CommWidget.buildEmptyView()
            : ListView.separated(
                itemBuilder: (c, i) {
                  var protocol = _protocolList?[i];
                  return _buildItem(protocol?.agreementName ?? '', () {
                    UrlHelper.loadH5Page(null, protocol?.agreementUrl ?? "");
                  });
                },
                separatorBuilder: (c, i) => Container(
                    color: ColorRes.white,
                    child: CommWidget.buildLine(
                        height: 1.w, margin: EdgeInsets.only(left: 16.w), color: ColorRes.colorF2F2F2)),
                itemCount: itemCount,
              ));
  }

  getProtocolList() async {
    var httpRequest = HttpRequest(
      Api.agreementList,
      body: {'systemType': Platform.operatingSystem, 'agreementSource': '1'},
    );
    var httpResponse = await HttpClient.postJson(httpRequest);
    var serviceResult = ServiceResult<List<Protocol>>.list(
        httpResponse, (map) => (map ?? []).map((e) => Protocol.fromJson(e)).toList());
    if (serviceResult.success) {
      _protocolList = serviceResult.responseBody;
      setState(() {});
    } else {
      ToastUtils.toast(serviceResult);
    }
  }

  GestureDetector _buildItem(String str, Function onTop) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          onTop();
        },
        child: Container(
            height: 56.w,
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            color: ColorRes.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  str,
                  style: TextStyle(color: ColorRes.color333333, fontSize: 15.sp),
                ),
                ImageLoader.loadAsset('icon_my_info_more.webp', 0, size: Size(8.w, 15.w))
              ],
            )));
  }
}
